package cn.xopencode.scoreservice.provider.demand;

import cn.xopencode.common.framework.validator.InEnum;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.score.api.enums.innovate.SuggestScoreEnum;
import cn.xopencode.score.api.enums.safety.ParticipatingRoleScoreEnum;
import cn.xopencode.score.api.enums.safety.RankingScoreEnum;
import cn.xopencode.score.api.rpc.ScoreRpc;
import cn.xopencode.score.api.rpc.demand.dto.ScoreDetailsRespDTO;
import cn.xopencode.score.api.rpc.demand.dto.ScoreReqDTO;
import cn.xopencode.score.api.rpc.demand.dto.ScoreRespDTO;
import cn.xopencode.scoreservice.service.demand.ScoreService;
import cn.xopencode.securityservice.enums.suggest.SuggestLevelEnum;
import cn.xopencode.securityservice.rpc.activity.dto.ActivityCreateReqDTO;
import cn.xopencode.securityservice.rpc.suggest.dto.SuggestCreateReqDTO;
import cn.xopencode.securityservice.rpc.violate.dto.ViolateCreateReqDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static cn.xopencode.common.framework.vo.CommonResult.success;

import java.util.Date;
import java.util.List;

/**
 * 安全活动与技能比武情况 Rpc 实现
 */
@DubboService(version = "${dubbo.provider.ScoreRpc.version}")
public class ScoreRpcProviderImpl implements ScoreRpc {
    @Autowired
    private ScoreService scoreService;

    @Override
    public CommonResult<ScoreDetailsRespDTO> getScoreDetails(String empId) {
        return success(scoreService.getDetailsScore(empId));
    }

    @Override
    public CommonResult<List<ScoreRespDTO>> getScoreList() {
        System.out.println("aaaaaaa");
//        System.out.println("scoreService.pageScore():"+scoreService.pageScore());

        return success(scoreService.pageScore());
    }

    @Override
    public CommonResult<List<ScoreRespDTO>> getDateScoreList(ScoreReqDTO scoreReqDTO) {
        return success(scoreService.pageScore());
    }

    /**
     * 创新与合理化建议
     * @param createDTO
     * @return 修改分的分数
     */
    @Override
    public Double addSuggestScore(@Valid SuggestCreateReqDTO createDTO) {
        Integer suggestLevel = createDTO.getSuggestLevel();
        Double resultScore = null;

        for (SuggestLevelEnum level : SuggestLevelEnum.values()) {
            if (createDTO.getSuggestLevel() == level.getLevel()) {
                for (SuggestScoreEnum score : SuggestScoreEnum.values()) {
                    if (level.getName().equals(score.getName())) {
                        System.out.println("name:" + score.getName());
                        System.out.println("getScore:" + score.getScore());
                        resultScore = scoreService.addSuggestScore("1001", score.getScore());
                    }
                }
            }
        }

        System.out.println("resultScore:" + resultScore);
        return resultScore;
    }

    /**
     * 安全活动与技能比武情况
     * @param createDTO
     * @return
     */
    @Override
    public Double addActivityScore(ActivityCreateReqDTO createDTO) {
        //1:参与人员,2:观摩人员
        Integer activityType = createDTO.getParticipatingRole();
        //1:公司第一名,2:公司第二名,3:公司第三名,4:通报表扬
        Integer ranking = createDTO.getRanking();
        Double resultScore = null;
        for(ParticipatingRoleScoreEnum participatingLevel : ParticipatingRoleScoreEnum.values()){
            if(participatingLevel.getParticipatingRole()==activityType){
                for (RankingScoreEnum rankingLevel : RankingScoreEnum.values()) {
                    if (rankingLevel.getRanking()==ranking) {
                        resultScore = participatingLevel.getScore()+rankingLevel.getScore();
                        System.out.println("activityType:"+activityType);
                        System.out.println("ranking:"+ranking);
                        scoreService.addActivityScore("1001",resultScore);
                    }
                }
            }
        }

        System.out.println("resultScore:" + resultScore);
        return resultScore;
    }

    /**
     * 反三违
     * @param createDTO
     * @return
     */
    @Override
    public Double updateViolateScore(ViolateCreateReqDTO createDTO) {
        //事故级别，1:班组级,2:车间级,3:公司级
        Integer violateLevel = createDTO.getViolateLevel();
        //获取责任人
        String personLiable = createDTO.getPersonLiable();
        //获取发现人
        String discoverer = createDTO.getDiscoverer();
        Double resultScore = null;
        if(violateLevel==1){
            //责任级别为班级组

            //传入发现人
            scoreService.updateViolateScore("1001",2.0);
            //传入责任人
            scoreService.updateViolateScore("1001",-2.0);
        }else if(violateLevel==2){
            //责任级别为车间级

            //传入责任人
            scoreService.updateViolateScore("1001",-3.0);
            //同班级组其他人 扣一分

        }else if(violateLevel==3){
            //责任级别为公司级

            //责任人和同班组全部积分清0
            scoreService.clearViolateScore("1001");
        }
        //传入发现人id
        ScoreDetailsRespDTO detailsScore = scoreService.getDetailsScore("1001");
        resultScore = detailsScore.getScore();
        System.out.println("发现人分数："+resultScore);
        return resultScore;
    }


}
